﻿using System;
using System.Drawing;
using System.Data;  
using System.Windows.Forms;
using DataGridViewAutoFilter;
using ProrabHelp.Library;


namespace ProrabHelp.UI
{
    public partial class Company : UserControl
    {
        ServerObject mServerObject = null;
        bool mNewRecord = false;

        public Company()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            mServerObject.CloseThisTab(this);   
        }

        private void FillCompanyGridView()
        {
            BindingSource bindigSource = new BindingSource(mServerObject.GetCompanyList(true), null);
            companyGridView.DataSource = bindigSource;

            if (companyGridView.DataSource != null)
            {
                foreach (DataGridViewColumn col in companyGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                companyGridView.AutoResizeColumns();
            }
        }

        private void Company_Load(object sender, EventArgs e)
        {
            FillCompanyGridView();

            tableLayoutPanel1.SetColumnSpan(companyGridView, 5);
            tableLayoutPanel2.Visible = false;
            infoLabel.Text = string.Empty;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(companyGridView, 4);
            tableLayoutPanel2.Visible = true;
            mNewRecord = true;

            idTextBox.Text = string.Empty;
            nameTextBox.Text = string.Empty;
            shortNameTextBox.Text = string.Empty;
            addressTextBox.Text = string.Empty;
            isActiveCheckBox.Checked = true;
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(companyGridView, 4);
            tableLayoutPanel2.Visible = true;

            if (companyGridView.SelectedRows.Count == 1)
            {                
                idTextBox.Text = companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["id"].Value.ToString();
                nameTextBox.Text = companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["Name"].Value.ToString();
                shortNameTextBox.Text = companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["Short_name"].Value.ToString();
                addressTextBox.Text = companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["Address"].Value.ToString();

                bool isDeleted = false;
                if (companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["isDeleted"].Value.ToString().Equals("True"))
                {
                    isDeleted = true;
                }

                isActiveCheckBox.Checked = !isDeleted; 
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(companyGridView, 5);
            tableLayoutPanel2.Visible = false;
            mNewRecord = false;

            idTextBox.Text = string.Empty;
            nameTextBox.Text = string.Empty;
            shortNameTextBox.Text = string.Empty;
            addressTextBox.Text = string.Empty;
            isActiveCheckBox.Checked = true;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(companyGridView, 5);
            tableLayoutPanel2.Visible = false;
            bool resultOperation = false;
            if (mNewRecord)
            {
                resultOperation = mServerObject.AddCompany(nameTextBox.Text.Trim(), shortNameTextBox.Text.Trim(), addressTextBox.Text.Trim(), !isActiveCheckBox.Checked);
            }
            else
            {
                int companyID = 0;
                if (companyGridView.SelectedRows.Count == 1)
                {
                    int.TryParse(companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out companyID);
                }

                if (companyID != 0)
                    resultOperation = mServerObject.UpdateCompany(companyID, nameTextBox.Text.Trim(), shortNameTextBox.Text.Trim(), addressTextBox.Text.Trim(), !isActiveCheckBox.Checked);
            }

            if (resultOperation)
            {
                infoLabel.Text = "Data saved Successful!";
            }
            else
            {
                infoLabel.Text = "Failure savind data!";
            }

            FillCompanyGridView();
            mNewRecord = false;
        }

        private void companyGridView_SelectionChanged(object sender, EventArgs e)
        {
            infoLabel.Text = string.Empty;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (companyGridView.SelectedRows.Count == 1 && MessageBox.Show("Are you sure???","Atention",MessageBoxButtons.OKCancel) == DialogResult.OK)
            {   
                int companyID = 0;
                int.TryParse(companyGridView.Rows[companyGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out companyID);
                bool resultOperation = false;
                resultOperation = mServerObject.DeleteCompany(companyID);

                if (resultOperation)
                { infoLabel.Text = "Record Succesifl deleted !"; }
                else
                { infoLabel.Text = "Error on the delete action !"; }

                FillCompanyGridView();
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            FillCompanyGridView();
        }

    }
}
